package com.boot.dao;

import com.boot.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class UserDao {

    @Autowired
    JdbcTemplate jdbcTemplate;

    public User getUser(User user){
        String sql = "select id,userName,password,birthday from t_user where userName=? and password=?";

        try{
            return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(User.class),user.getUserName(),user.getPassword());
        }catch (Exception e){
            return null;
        }

    }

    public List<User> list() {
        String sql = "select id,userName,password,birthday from t_user limit 0,10";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(User.class));
    }

    public void deleteById(Long id) {
        String sql = "delete from t_user where id = ?";
        jdbcTemplate.update(sql,id);
    }
}
